<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author XPF
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" v-auth="'sports:sportsSingleIndex:edit'" @click="handleExport()">
          <Icon icon="ant-design:download-outlined" /> {{ t('模板下载') }}
        </a-button>
        <a-button type="default" @click="handleImport()" v-auth="'sports:sportsSingleIndex:edit'">
          <Icon icon="ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button>
        <a-button type="primary" @click="handleForm({})" v-auth="'sports:sportsSingleIndex:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ scoreId: record.scoreId })">
          {{ record.scoreAdaptSex }}
        </a>
      </template>
      <template #scoreGrade="{ column, record }">
        <DictLabel
          v-if="record.sportsScoreGrade"
          :dictType="column.dictType"
          :dictValue="record.sportsScoreGrade.scoreGrade"
        />
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
    <DrawerFrom @register="registerGrade" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsSportsSportsSingleIndexList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { sportsSingleIndexDelete, sportsSingleIndexListData } from '/@/api/sports/sportsSingleIndex';
  import { selectData } from '/@/api/sports/sportsWeight';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { useModal } from '/@/components/Modal';
  import { DictLabel } from '/@/components/Dict';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';
  import DrawerFrom from './drawer.vue';

  const { t } = useI18n('sports.sportsSingleIndex');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('单项指标对应评分&单项得分&等级管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 120,
    schemas: [
      {
        label: t('关联单项指标'),
        field: 'indexId',
        component: 'Select',
        componentProps: {
          allowClear: true,
          api: async () => {
            let res = await selectData({});
            let data = [];
            res.data.forEach((item, index) => {
              data.push({ label: item.indexName, value: item.indexId });
            });
            return data;
          },
        },
      },
      {
        label: t('评分适配性别'),
        field: 'scoreAdaptSex',
        component: 'Select',
        componentProps: {
          dictType: 'sys_user_sex',
        },
      },
      {
        label: t('评分适配年级'),
        field: 'scoreAdaptGrade',
        component: 'Select',
        componentProps: {
          dictType: 'vke_grade',
        },
      },
      
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('指标名称'),
      dataIndex: 'sportsWeight.indexName',
      key: 'a.index_id',
      //sorter: true,
      width: 130,
      align: 'left',
      dictType: '',
    },
    {
      title: t('适配年级'),
      dataIndex: 'scoreAdaptGrade',
      key: 'a.score_adapt_grade',
      //sorter: true,
      width: 130,
      align: 'center',
      dictType: 'vke_grade',
    },
    {
      title: t('适配性别'),
      dataIndex: 'scoreAdaptSex',
      key: 'a.score_adapt_sex',
      //sorter: true,
      width: 80,
      align: 'center',
      dictType: 'sys_user_sex',
    },
    // {
    //   title: t('符号'),
    //   dataIndex: 'scoreSymbol',
    //   key: 'a.score_symbol',
    //   //sorter: true,
    //   width: 80,
    //   align: 'left',
    //   dictType: 's_scoring_rules',
    // },
    {
      title: t('分值'),
      dataIndex: 'scoreTotal',
      key: 'a.score_1',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('单项得分'),
      dataIndex: 'sportsScoreGrade.singleScore',
      key: 'a.score_1',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('等级'),
      dataIndex: 'sportsScoreGrade.scoreGrade',
      key: 's.score_grade',
      //sorter: true,
      width: 90,
      align: 'center',
      dictType: 's_score_grade',
      slots: { customRender: 'scoreGrade' },
    },
    {
      title: t('更新时间'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      //sorter: true,
      width: 130,
      align: 'center',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑单项评分规则'),
        onClick: handleForm.bind(this, { scoreId: record.scoreId, indexId: record.indexId }),
        auth: 'sports:sportsSingleIndex:edit',
      },
      {
        icon: 'ant-design:crown-outlined',
        title: t('关联等级'),
        //label: t('关联等级'),
        onClick: handleGrade.bind(this, {
          scoreId: record.scoreId,
          scoreGradeId: record.scoreGradeId,
        }),
        auth: 'sports:sportsSingleIndex:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除单项评分规则'),
        popConfirm: {
          title: t('是否确认删除单项评分规则'),
          confirm: handleDelete.bind(this, { scoreId: record.scoreId }),
        },
        auth: 'sports:sportsSingleIndex:edit',
      },
    ],
  };

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      // url: ctxAdminPath + '/sys/empUser/exportData',
      url: ctxAdminPath + '/sports/sportsSingleIndex/exportData',
      target: '_blank',
    });
  }
  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload }] = useTable({
    api: sportsSingleIndexListData,
    beforeFetch: (params) => {
      return params;
    },
    afterFetch: (res) => {
      res.forEach((item) => {
        if (item.score2 != '') {
          item.scoreTotal = item.score1 + '-' + item.score2;
        } else {
          item.scoreTotal = item.score1;
        }
      });
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  const [registerGrade, { openDrawer: openGrade }] = useDrawer();
  function handleGrade(record: Recordable) {
    openGrade(true, record);
  }

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await sportsSingleIndexDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
